package com.gzs.learn.admin.web.controller.gen;

import com.gzs.learn.admin.common.annotation.Log;
import com.gzs.learn.admin.common.core.controller.BaseController;
import com.gzs.learn.admin.common.core.page.TableDataInfo;
import com.gzs.learn.admin.common.enums.BusinessType;
import com.gzs.learn.admin.common.utils.poi.ExcelUtil;
import com.gzs.learn.admin.model.po.DbConfig;
import com.gzs.learn.admin.service.IDbConfigService;
import com.gzs.learn.common.AjaxResult;
import com.gzs.learn.inf.PageResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 数据源配置Controller
 *
 * @author guanzhisong
 * @date 2021-08-12
 */
@RestController
@RequestMapping("/tool/dbconfig")
@RequiredArgsConstructor
public class DbConfigController extends BaseController {
    private final IDbConfigService dbConfigService;

    /**
     * 查询数据源配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:dbconfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(DbConfig dbConfig) {
        PageResponseDto<DbConfig> responseDto = dbConfigService.selectDbConfigList(dbConfig);
        return getDataTable(responseDto);
    }

    /**
     * 导出数据源配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:dbconfig:export')")
    @Log(title = "数据源配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DbConfig dbConfig) {
        PageResponseDto<DbConfig> responseDto = dbConfigService.exportDbConfigList(dbConfig);
        ExcelUtil<DbConfig> util = new ExcelUtil<DbConfig>(DbConfig.class);
        return util.exportExcel(responseDto.getData(), "dbconfig");
    }

    /**
     * 获取数据源配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:dbconfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(dbConfigService.selectDbConfigById(id));
    }

    /**
     * 获取数据源配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:dbconfig:query')")
    @GetMapping(value = "/jdbc_list")
    public AjaxResult jdbcList() {
        PageResponseDto<DbConfig> exportDbConfigList = dbConfigService.exportDbConfigList(new DbConfig());
        return AjaxResult.success(exportDbConfigList.getData());
    }

    /**
     * 新增数据源配置
     */
    @PreAuthorize("@ss.hasPermi('system:dbconfig:add')")
    @Log(title = "数据源配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DbConfig dbConfig) {
        return toAjax(dbConfigService.insertDbConfig(dbConfig));
    }

    /**
     * 修改数据源配置
     */
    @PreAuthorize("@ss.hasPermi('system:dbconfig:edit')")
    @Log(title = "数据源配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DbConfig dbConfig) {
        return toAjax(dbConfigService.updateDbConfig(dbConfig));
    }

    /**
     * 修改数据源配置
     */
    @PreAuthorize("@ss.hasPermi('tool:dbconfig:edit')")
    @Log(title = "数据源配置", businessType = BusinessType.UPDATE)
    @PostMapping("change_status")
    public AjaxResult changeStatus(Long id, Integer status) {
        return toAjax(dbConfigService.changeStatus(id, status));
    }

    /**
     * 删除数据源配置
     */
    @PreAuthorize("@ss.hasPermi('system:dbconfig:remove')")
    @Log(title = "数据源配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(dbConfigService.deleteDbConfigByIds(ids));
    }
}
